package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.Agent;
import org.hgvc.webapp.ets.constant.AgentConstant;
import org.hgvc.webapp.ets.constant.SystemConstant;
import org.hgvc.webapp.ets.jsf.beans.agent.AdminCommissionBean;
import org.hgvc.webapp.ets.jsf.beans.agent.AgentAgreementTransferBean;
import org.hgvc.webapp.ets.spring.service.AgentService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.FacesUtils;



public class AdminAgentMgmtBean extends AbstractBean {

	public static final String BEAN_NAME="adminAgentMgmtBean";
	public static final String NAV_LIST_ACTION="adminAgentMgmt";
	private AgentService service;
	private List<Agent> agentList;
	private String regno;
	private String name;
	private Agent agent;
	public void setService(AgentService service) {
		this.service = service;
	}

	public void setAgentList(List<Agent> agentList) {
		this.agentList = agentList;
	}
	public String getRegno() {
		return regno;
	}
	public void setRegno(String regno) {
		this.regno = regno;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	/*------------------------------------------------*/
	public List<Agent> getAgentList() {
		try{
			agentList = service.getAgentListByType(AgentConstant.AGENT_LEVEL_1);
			agentList = service.searchRegNoPartnerNameInList(regno, name, agentList);
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return agentList;
	}
	
	public String navListAction(){
		return NAV_LIST_ACTION.concat("_"+getRoleName());
	}
	//added by jimmy for terminate agent agreement transfer
	public String navAgreementTransfer(){
		return AgentAgreementTransferBean.NAV_LIST.concat("_"+getRoleName());
	}
	public void navAgreementTransferListener(ActionEvent actionEvent) throws Exception{
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("agentId2");
		String id = (String)param.getValue();
		AgentAgreementTransferBean agentAgreementTransferBean = (AgentAgreementTransferBean)FacesUtils.getManagedBean(AgentAgreementTransferBean.BEAN_NAME);
		agentAgreementTransferBean.setAgent((Agent)commonDao.getObject(Agent.class, id));

		agentAgreementTransferBean.iniBean();
		
	}
	 
	public void terminateActionListener(ActionEvent actionEvent) throws Exception{
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("agentId3");
		String id = (String)param.getValue();
		service.saveTerminateAgent(id, getThisUser());
		
	}
	public void comAction() throws Exception{
		AdminCommissionBean adminCommissionBean = (AdminCommissionBean)FacesUtils.getManagedBean("adminCommissionBean");
		adminCommissionBean.run();
	}
	
	public boolean isCanTransferAgreement(){
		if (getRoleName().equalsIgnoreCase(SystemConstant.ADMIN_ROLE.getName()) ||
				getRoleName().equalsIgnoreCase(SystemConstant.MEMBER_SERVICE_ROLE.getName()))
			return true ;
		return false;
	}
	////////////////
	
	public String searchAction(){
		return NAV_LIST_ACTION.concat("_"+getRoleName());
	}
	
	public String backAction(){
		return AdminPortalBean.NAV_ADMIN_PORTAL;
	}
	
	public void agreementControlActionListener(ActionEvent actionEvent){
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("id");
		String id = (String)param.getValue();
		for(Agent agent:agentList){
			if(agent.getAgentId().equalsIgnoreCase(id)){
				this.agent = agent;
				break;
			}
		}
	}
	
	public String navAgreementControl(){
		AdminAgreementControlBean bean = (AdminAgreementControlBean)FacesUtils.getManagedBean(AdminAgreementControlBean.BEAN_NAME);
		return bean.navListAction(agent);
	}
	
	public void agentCommActionListener(ActionEvent actionEvent){
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("aid");
		String id = (String)param.getValue();
		for(Agent agent:agentList){
			if(agent.getAgentId().equalsIgnoreCase(id)){
				this.agent = agent;
				break;
			}
		}
	}
	
	public String navAgentComm(){
		
		AdminAgentCommBean bean = (AdminAgentCommBean)FacesUtils.getManagedBean(AdminAgentCommBean.BEAN_NAME);
		return bean.navListAction(agent);
	}
	
	public void agentProfileActionListener(ActionEvent actionEvent){
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("profileid");
		String id = (String)param.getValue();
		for(Agent agent:agentList){
			if(agent.getAgentId().equalsIgnoreCase(id)){
				this.agent = agent;
				break;
			}
		}
	}
	
	public String navAgentProfile(){
		
		AdminAgentProfileBean bean = (AdminAgentProfileBean)FacesUtils.getManagedBean(AdminAgentProfileBean.BEAN_NAME);
		return bean.navViewAction(agent);
	}
}
